package com.embhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.entity.LogException.CheckItemDeleteFailException;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.pojo.CheckItem;
import com.embhealth.dao.CheckItemDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.embhealth.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    /**
     * 检查项分页查询
     */
    public PageResult findPage(QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    /**
     * 根据id删除检查项
     */
    public void deleteById(Integer id) throws CheckItemDeleteFailException {
        //判断检查项是否管理刀检查组
        int count = checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            throw new CheckItemDeleteFailException();
        }
        checkItemDao.deleteById(id);
    }

    @Override
    /**
     * 更新表单项
     */
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    /**
     * 根据id查询检查项信息
     */
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    /**
     * 查询全部检查项信息，用于新增检查组和编辑检查组
     */
    public PageResult findAll(Integer id,QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkItemDao.findAll(id);
        long total = page.getTotal();
        List<CheckItem> result = page.getResult();
        return new PageResult(total,result);
    }
}
